/*	
 * Authors: Ken YANG <spng.yang@gmail.com>
 * 	
 * Introduction
 * Set various socket options
 *
 * Copyright (C) 2007-2008 Ken YANG
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation, version 2. 
 * THIS SOFTWARE COMES "AS IS" WITHOUT ANY WARRANTIES.
 */

#include "mmh.h"

/*
 * Name: mmh_sockopts()
 *
 * Description: 
 * Set various socket options
 *
 * Parameters: 
 * sockfd:	Socket to set options;
 * setall_f:	Flag to indicate what option can not set currently;
 *
 * Return Values:
 * None
 *
 * */
void 
mmh_sockopts(int sockfd, int setall_f)
{
	int optvalue, optlen;

	/* 
	 * Set TCP MSS(if user specified), before connection 
	 * established. 
	 * XXX: some kernel dont let the process set this 
	 * socket options, and others only let it be decreased
	 * */
	if(mmh_tcpmss && mmh_udp_f==0){
		Setsockopts(sockfd,IPPROTO_TCP,TCP_MAXSEG,
				&mmh_tcpmss,sizeof(mmh_tcpmss));
		optlen=sizeof(optvalue);
		Getsockopt(sockfd,IPPROTO_TCP,TCP_MAXSEG,
				&optvalue,&optlen);
		if(optvalue!=mmh_tcpmss)
			err_quit("TCP_MAXSEG=%d, but you specified: %d", optvalue,mmh_tcpmss);
		if(mmh_verbose)
			err_msg("TCP_MAXSEG = %d",n);
	}

	/* Set to allow to broadcast */
	if(mmh_broadcast){
		/* dont use mmh_boradcast, since it's global */
		option=1;
		Setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&option,
				sizeof(option));
		option=0;
		optlen=sizeof(option);
		Getsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&option,
				&optlen);
		if(option==0)
			err_quit("SO_BROADCAST not set (%d)",option);
		if(mmh_verbose)
			err_msg("SO_BROADCAST set");
	}

	/* 
	 * TODO: there are left many options not to set
	 * */
}


